<?php
namespace Home\Controller;
use Think\Controller;
class AdministrationController extends CommunalController{
	//物资库 designed by:zhaoshihao 2018-01-17
	function material(){
		parent::check_permission('WZ');
		$db_appliance=M('db_appliance');
		$db_appliance_goods=M('db_appliance_goods');
		if(!empty($_REQUEST['type'])){$type=$_REQUEST['type'];}else{$type=-10;}
		$this->assign('type',$type);
		if(!empty($_REQUEST['keyword']))
		{
			$map['number']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['name']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['_logic']='or';
			$where['_complex']=$map;
			$this->assign('keyword',$_REQUEST['keyword']);
		}
		if($type!=-10){$where['type']=$type;}
		$count=$db_appliance_goods->where($where)->count();
		$Page = new \Think\Page($count,15);
		$show=$Page->show();
		$result=$db_appliance_goods->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($result as $key=>$value){
			$where_g['gid']=$value['id'];
			$have=$db_appliance->where($where_g)->find();
			if(empty($have)){$result[$key]['audit']=1;}
			$where_r['gid']=$value['id'];
			$where_r['types']=1;
			$result[$key]['rk']=$db_appliance->where($where_r)->sum('nums');
			$where_r['types']=2;
			$result[$key]['ck']=$db_appliance->where($where_r)->sum('nums');
			if(!empty($result[$key]['rk']) or !empty($result[$key]['ck'])){$result[$key]['sy']=$result[$key]['rk']-$result[$key]['ck'];}
		}
		$this->assign('page',$show);
		$this->assign("result",$result);
		$this->assign("user",$this->get_all_username(1));
		$this->display();
	}
	
	//物资库-->添加 designed by:zhaoshihao 2018-01-17
	function material_add(){
		if($_REQUEST['action']=='doadd'){
			$db_appliance_goods=M('db_appliance_goods');
			if($_REQUEST['type']==1){
				$field='MDLP';
			}elseif($_REQUEST['type']==2){
				$field='MDYP';
			}
			$where_s['number']=array('like',$field.'%');
			$result=$db_appliance_goods->field('number')->where($where_s)->order('number desc')->find();
			if(empty($result))
			{
				$item=$field.'0001';
			}
			else
			{
				$item=substr($result['number'],4)+1;
				if(strlen($item)==1){$item='000'.$item;}
				elseif(strlen($item)==2){$item='00'.$item;}
				elseif(strlen($item)==3){$item='0'.$item;}
			}
			$where['number']=$field.$item;
			$where['type']=$_REQUEST['type'];
			$where['name']=$_REQUEST['name'];
			$where['unit']=$_REQUEST['unit'];
			$where['price']=$_REQUEST['price'];
			if(!empty($_REQUEST['norms'])){$where['norms']=$_REQUEST['norms'];}
			if(!empty($_REQUEST['norms'])){$where['brand']=$_REQUEST['brand'];}
			$where['add_uid']=$this->login_id;
			$where['add_date']=date('Y-m-d H:i:s');
			$db_appliance_goods->add($where);
			$this->success("操作成功",__APP__."/Administration/material");die;
		}
		$this->display();
	}
	
	//物资库-->编辑 designed by:zhaoshihao 2018-01-17
	function material_edit(){
		$db_appliance_goods=M('db_appliance_goods');
		$db_appliance=M('db_appliance');
		$where['id']=$_REQUEST['id'];
		$result=$db_appliance_goods->where($where)->find();
		$this->assign("result",$result);
		$where_g['gid']=$_REQUEST['id'];
		$have=$db_appliance->where($where_g)->find();
		$this->assign("have",$have);
		if($_REQUEST['action']=='doedit'){
			if(empty($have)){
				$where_a['type']=$_REQUEST['type'];
				$where_a['name']=$_REQUEST['name'];
				if(!empty($_REQUEST['norms'])){$where_a['norms']=$_REQUEST['norms'];}else{$where_a['norms']=NULL;}
				if(!empty($_REQUEST['brand'])){$where_a['brand']=$_REQUEST['brand'];}else{$where_a['brand']=NULL;}
			}
			$where_a['id']=$_REQUEST['id'];
			$where_a['unit']=$_REQUEST['unit'];
			$where_a['price']=$_REQUEST['price'];
			$db_appliance_goods->save($where_a);
			$this->success("操作成功",__APP__."/Administration/material/keyword/".$_REQUEST['keyword']."&p=".$_SESSION['page']);die;
		}
		$this->display();
	}
	
	//物资库-->详情 designed by:zhaoshihao 2018-01-17
	function material_detail(){
		$db_appliance_goods=M('db_appliance_goods');
		$db_appliance=M('db_appliance');
		$where['id']=$_REQUEST['id'];
		$result=$db_appliance_goods->where($where)->find();
		$where_r['gid']=$_REQUEST['id'];
		$where_r['types']=1;
		$result['rk']=$db_appliance->where($where_r)->sum('nums');
		$where_r['types']=2;
		$result['ck']=$db_appliance->where($where_r)->sum('nums');
		if(!empty($result['rk']) or !empty($result['ck'])){$result['sy']=$result['rk']-$result['ck'];}
		$this->assign("result",$result);
		$this->display();
	}
	
	//物资库-->ajax designed by:zhaoshihao 2018-01-17
	function material_ajax(){
		$db_appliance=M('db_appliance');
		$db_appliance_goods=M('db_appliance_goods');
		if($_REQUEST['action']=='doadd'){
			$where['type']=$_REQUEST['type'];
			$where['name']=$_REQUEST['name'];
			if(!empty($_REQUEST['norms'])){$where['norms']=$_REQUEST['norms'];}
			if(!empty($_REQUEST['brand'])){$where['brand']=$_REQUEST['brand'];}
			$have=$db_appliance_goods->where($where)->find();
			if(!empty($have)){
				$data['status']=self::NO;
				$data['msg']='已有相同物资，请勿重复添加';
				echo json_encode($data);die;
			}else{
				$data['status']=self::YES;
				echo json_encode($data);die;
			}
		}elseif($_REQUEST['action']=='doedit'){
			//物品是否重复
			$where['id']=array('neq',$_REQUEST['id']);
			$where['type']=$_REQUEST['type'];
			$where['name']=$_REQUEST['name'];
			if(!empty($_REQUEST['norms'])){$where['norms']=$_REQUEST['norms'];}
			if(!empty($_REQUEST['brand'])){$where['brand']=$_REQUEST['brand'];}
			$has=$db_appliance_goods->where($where)->find();
			if(!empty($has)){
				$data['status']=self::NO;
				$data['msg']='已有相同物资,请修改';
				echo json_encode($data);die;
			}else{
				//是否有进出库数据
				$where_g['gid']=$_REQUEST['id'];
				$have=$db_appliance->where($where_g)->find();
				$where_s['id']=$_REQUEST['id'];
				$good=$db_appliance_goods->where($where_s)->find();
				if(empty($have)){
					$data['status']=self::YES;
					echo json_encode($data);die;
				}else{
					if($good['name']!=$_REQUEST['name']){
						$data['status']=self::NO;
						$data['msg']='已有单据，不能修改名称';
						echo json_encode($data);die;
					}else{
						$data['status']=self::YES;
						echo json_encode($data);die;
					}
				}
			}
		}elseif($_REQUEST['action']=='delete'){
			$where_g['gid']=$_REQUEST['id'];
			$have=$db_appliance->where($where_g)->find();
			if(empty($have)){
				$where['id']=$_REQUEST['id'];
				$db_appliance_goods->where($where)->delete();
				$data['status']=self::YES;
				echo json_encode($data);die;
			}else{
				$data['status']=self::NO;
				$data['msg']='已有单据，不能删除';
				echo json_encode($data);die;
			}
		}
	}
	
	//物资库-->流水 designed by:zhaoshihao 2018-01-17
	function material_change(){
		$db_appliance=M('db_appliance');
		$db_appliance_goods=M('db_appliance_goods');
		if(!empty($_REQUEST['types'])){$types=$_REQUEST['types'];}else{$types=-10;}
		$this->assign('types',$types);
		if(!empty($_REQUEST['syb'])){$syb=$_REQUEST['syb'];}else{$syb=-10;}
		$this->assign('syb',$syb);
		if(!empty($_REQUEST['start'])){$start=$_REQUEST['start'];}else{$start=date('Y-01-01');}
		$this->assign('start',$start);
		if(!empty($_REQUEST['end'])){$end=$_REQUEST['end'];}else{$end=date('Y-m-d');}
		$this->assign('end',$end);
		if($types!=-10){$where['types']=$types;}
		if($syb!=-10){$where['business_unit']=$syb;}
		$where['k_date']=array('between',array($start,$end));
		$where['gid']=$_REQUEST['id'];
		$count=$db_appliance->where($where)->count();
		$Page = new \Think\Page($count,15);
		$show=$Page->show();
		$result=$db_appliance->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($result as $key=>$value){
			$result[$key]['add_user']=$this->get_username($value['add_uid']);
			if(empty($value['user_name'])){$result[$key]['user_name']=$this->get_username($value['user']);}
			if($value['types']==1){
				$where_r['gid']=$value['gid'];
				$where_r['types']=1;
				$rk=$db_appliance->where($where_r)->sum('nums');
				$where_r['types']=2;
				$ck=$db_appliance->where($where_r)->sum('nums');
				$sy=$rk-$ck;
				if($sy>$value['nums']){$result[$key]['del']=1;}
			}else{
				$result[$key]['del']=1;
			}
			if(($key==0) and (($value['add_uid']==$this->login_id) or ($this->login_id==1))){$result[$key]['audit']=1;}
			if($key==0){
				if($value['types']==2){
					$l_cj=$value['nums'];
				}
			}
		}
		$where_g['id']=$_REQUEST['id'];
		$goods=$db_appliance_goods->where($where_g)->find();
		$this->assign('goods',$goods);
		$this->assign('page',$show);
		$this->assign('id',$_REQUEST['id']);
		$this->assign("result",$result);
		$this->assign("user",$this->get_all_username(1));
		
		$where_r['gid']=$_REQUEST['id'];
		$where_r['types']=1;
		$rk=$db_appliance->where($where_r)->sum('nums');
		$where_r['types']=2;
		$ck=$db_appliance->where($where_r)->sum('nums');
		if(!empty($rk) or !empty($ck)){$sy=$rk-$ck+$l_cj;}
		$this->assign("sy",$sy);
		$this->display();
	}
	
	//物资库-->流水ajax designed by:zhaoshihao 2018-01-17
	function appliance_list_ajax(){
		$db_appliance_goods=M('db_appliance_goods');
		$db_appliance=M('db_appliance');
		if($_REQUEST['action']=='add'){
			//流水添加
			if($_REQUEST['types']==1){
				$where['types']=$_REQUEST['types'];
				$where['gid']=$_REQUEST['id'];
				$where['business_unit']=$_REQUEST['business_unit'];
				$where['manufacturer']=$_REQUEST['manufacturer'];
				$where['nums']=$_REQUEST['nums1'];
				$where['prices']=$_REQUEST['prices'];
				$where['sum']=$_REQUEST['nums1']*$_REQUEST['prices'];
				$where['add_uid']=$this->login_id;
				$where['k_date']=date('Y-m-d');
				$where['add_date']=date('Y-m-d H:i:s');
				$db_appliance->add($where);
				$data['status']=self::YES;
				echo json_encode($data);die;
			}else{
				$where['gid']=$_REQUEST['id'];
				$where['types']=1;
				$rk=$db_appliance->where($where)->sum('nums');
				$where['types']=2;
				$ck=$db_appliance->where($where)->sum('nums');
				$sy=$rk-$ck;
				if($sy-$_REQUEST['nums']<0){
					$data['status']=self::NO;
					$data['msg']='超出库存剩余数'.$sy;
					echo json_encode($data);die;
				}else{
					$where['types']=$_REQUEST['types'];
					$where['gid']=$_REQUEST['id'];
					$where['business_unit']=$_REQUEST['business_unit'];
					$where['nums']=$_REQUEST['nums'];
					$where['prices']=$_REQUEST['pricess'];
					$where['sum']=$_REQUEST['nums']*$_REQUEST['pricess'];
					$where['user']=$_REQUEST['user'];
					$where['purpose']=$_REQUEST['purpose'];
					$where['add_uid']=$this->login_id;
					$where['k_date']=date('Y-m-d');
					$where['add_date']=date('Y-m-d H:i:s');
					$db_appliance->add($where);
					$data['status']=self::YES;
					echo json_encode($data);die;
				}
			}
		}elseif($_REQUEST['action']=='edit'){
			//流水编辑，只限最新一条
			$where_a['id']=$_REQUEST['id'];
			$del=$db_appliance->where($where_a)->find();
			$where_h['gid']=$del['gid'];
			$ha=$db_appliance->where($where_h)->order('id desc')->find();
			if($ha['id']!=$_REQUEST['id']){
				$data['status']=self::NO;
				$data['msg']='不是最新一条流水，不能编辑';
				echo json_encode($data);die;
			}else{
				if($del['types']==1){
					$where['gid']=$del['gid'];
					$where['types']=1;
					$where['id']=array('neq',$_REQUEST['id']);
					$rk=$db_appliance->where($where)->sum('nums');
					$where['types']=2;
					$ck=$db_appliance->where($where)->sum('nums');
					$sy=$rk-$ck;
					if($sy+$_REQUEST['nums']<0){
						$min=0-$sy;
						$data['status']=self::NO;
						$data['msg']='不能小于最小数量'.$min;
						echo json_encode($data);die;
					}else{
						$where_s['id']=$_REQUEST['id'];
						$where_s['business_unit']=$_REQUEST['business_unit'];
						$where_s['manufacturer']=$_REQUEST['manufacturer'];
						$where_s['nums']=$_REQUEST['nums1'];
						$where_s['prices']=$_REQUEST['prices'];
						$where_s['sum']=$_REQUEST['nums1']*$_REQUEST['prices'];
						$db_appliance->save($where_s);
						$data['status']=self::YES;
						echo json_encode($data);die;
					}
				}elseif($del['types']==2){
					$where['gid']=$del['gid'];
					$where['types']=1;
					$rk=$db_appliance->where($where)->sum('nums');
					$where['types']=2;
					$where['id']=array('neq',$_REQUEST['id']);
					$ck=$db_appliance->where($where)->sum('nums');
					$sy=$rk-$ck;
					if($sy-$_REQUEST['nums']<0){
						$data['status']=self::NO;
						$data['msg']='不能小于最小数量'.$sy;
						echo json_encode($data);die;
					}else{
						$where_s['id']=$_REQUEST['id'];
						$where_s['business_unit']=$_REQUEST['business_unit'];
						$where_s['nums']=$_REQUEST['nums'];
						$where_s['prices']=$_REQUEST['pricess'];
						$where_s['sum']=$_REQUEST['nums']*$_REQUEST['pricess'];
						$where_s['user']=$_REQUEST['user'];
						$where_s['purpose']=$_REQUEST['purpose'];
						$db_appliance->save($where_s);
						$data['status']=self::YES;
						echo json_encode($data);die;
					}
				}
			}
		}elseif($_REQUEST['action']=='delete'){
			//流水删除，只限最新一条
			$where_a['id']=$_REQUEST['id'];
			$del=$db_appliance->where($where_a)->find();
			$where_h['gid']=$del['gid'];
			$ha=$db_appliance->where($where_h)->order('id desc')->find();
			if($ha['id']!=$_REQUEST['id']){
				$data['status']=self::NO;
				$data['msg']='不是最新一条流水，不能删除';
				echo json_encode($data);die;
			}else{
				if($del['types']==1){
					$where['gid']=$del['gid'];
					$where['types']=1;
					$where['id']=array('neq',$_REQUEST['id']);
					$rk=$db_appliance->where($where)->sum('nums');
					$where['types']=2;
					$ck=$db_appliance->where($where)->sum('nums');
					$sy=$rk-$ck;
					if($sy<0){
						$data['status']=self::NO;
						$data['msg']='少于最小库存，不能删除';
						echo json_encode($data);die;
					}else{
						$where_d['id']=$_REQUEST['id'];
						$db_appliance->where($where_d)->delete();
						$data['status']=self::YES;
						echo json_encode($data);die;
					}
				}else{
					$where_d['id']=$_REQUEST['id'];
					$db_appliance->where($where_d)->delete();
					$data['status']=self::YES;
					echo json_encode($data);die;
				}
			}
		}
	}
	
	//全部流水 designed by:zhaoshihao 2018-01-17
	function material_change_all(){
		$db_appliance=M('db_appliance');
		$db_appliance_goods=M('db_appliance_goods');
		if(!empty($_REQUEST['types'])){$types=$_REQUEST['types'];}else{$types=-10;}
		$this->assign('types',$types);
		if(!empty($_REQUEST['syb'])){$syb=$_REQUEST['syb'];}else{$syb=-10;}
		$this->assign('syb',$syb);
		if(!empty($_REQUEST['start'])){$start=$_REQUEST['start'];}else{$start=date('Y-01-01');}
		$this->assign('start',$start);
		if(!empty($_REQUEST['end'])){$end=$_REQUEST['end'];}else{$end=date('Y-m-d');}
		$this->assign('end',$end);
		if(!empty($_REQUEST['gid'])){$gid=$_REQUEST['gid'];}else{$gid=-10;}
		$this->assign('gid',$gid);
		if($types!=-10){$where['types']=$types;}
		if($syb!=-10){$where['business_unit']=$syb;}
		if($gid!=-10){$where['gid']=$gid;}
		$where['k_date']=array('between',array($start,$end));
		$count=$db_appliance->where($where)->count();
		$Page = new \Think\Page($count,15);
		$show=$Page->show();
		$result=$db_appliance->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($result as $key=>$value){
			$result[$key]['add_user']=$this->get_username($value['add_uid']);
			if(empty($value['user_name'])){$result[$key]['user_name']=$this->get_username($value['user']);}
			if($value['types']==1){
				$where_r['gid']=$value['gid'];
				$where_r['types']=1;
				$rk=$db_appliance->where($where_r)->sum('nums');
				$where_r['types']=2;
				$ck=$db_appliance->where($where_r)->sum('nums');
				$sy=$rk-$ck;
				if($sy>$value['nums']){$result[$key]['del']=1;}
			}else{
				$result[$key]['del']=1;
			}
			$where_g['id']=$value['gid'];
			$name=$db_appliance_goods->where($where_g)->find();
			$result[$key]['name']=$name['name'];
			$result[$key]['number']=$name['number'];
		}
		unset($where['gid']);
		$goods=$db_appliance_goods->where($where)->join('left join db_appliance on db_appliance_goods.id=db_appliance.gid')->group('db_appliance_goods.id')->select();
		$this->assign('goods',$goods);
		$this->assign('page',$show);
		$this->assign("result",$result);
		$this->display();
	}
	
	//座位表 designed by:zhaoshihao 2018-01-24
	function seat_map(){
		parent::check_permission('ZW');
		$db_seat_map=M('db_seat_map');
		$db_seat_map_user=M('db_seat_map_user');
		//第一块区
		$where1['region']=1;
		$where1['region_name']='工位';
		$seat1=$db_seat_map->where($where1)->select();
		foreach($seat1 as $key=>$val){
			$seat=explode('-',$val['seat_number']);
			if(count($seat)==2){
				$seat1[$key]['seat_id']='seat'.($seat[1]+0);
				$seat1[$key]['span']=$seat[1];
			}else{
				$seat1[$key]['seat_id']='seat'.($seat[0]+0);
				$seat1[$key]['span']=$seat[0];
			}
			$where_s['sid']=$val['id'];
			$aaa=$db_seat_map_user->where($where_s)->find();
			$seat1[$key]['u_id']=$aaa['id'];
			if(!empty($aaa['uids'])){$seat1[$key]['users']=$this->get_username($aaa['uids']);}else{$seat1[$key]['users']=$aaa['users'];}
		}
		$this->assign("seat1",$seat1);
		$where1['region_name']=array('neq','工位');
		$seat11=$db_seat_map->where($where1)->select();
		//$seat11=$db_seat_map->where($where1)->join('left join db_seat_map_user on db_seat_map.id=db_seat_map_user.sid')->join('left join db_seat_map_thing on db_seat_map_user.id=db_seat_map_thing.gid')->select();
		foreach($seat11 as $key=>$val){
			$where_s['sid']=$val['id'];
			$as=$db_seat_map_user->where($where_s)->select();
			foreach($as as $kc=>$vc){
				if(!empty($vc['uids'])){$as[$kc]['users']=$this->get_username($vc['uids']);}
				if(count($as)>1){
					$as[$kc]['seat_id']='room_'.$val['seat_number'].($kc+1);
				}else{
					if($val['seat_number']==604){
						$as[$kc]['seat_id']='room_'.$val['seat_number'].($kc+1);
						$sum1=(count($as));
						$as[($sum1+1)]['seat_id']='room_'.$val['seat_number'].'2';
						$as[($sum1+1)]['sid']=$vc['sid'];
					}else{
						$as[$kc]['seat_id']='room_'.$val['seat_number'];
					}
				}
				if($val['region_name']=='办公室'){
					$as[$kc]['rname']=$val['seat_number'];
					$as[$kc]['em']=1;
				}else{
					$as[$kc]['rname']=$vc['users'];
				}
				if($val['seat_number']==604 and count($sum1)==1){
					$ss1[]=$as[$kc];
					$ss1[]=$as[(count($as))];
				}else{
					$ss1[]=$as[$kc];
				}
			}
		}
		$this->assign("ss1",$ss1);
		//第二块区
		$where2['region']=2;
		$where2['region_name']='工位';
		$seat2=$db_seat_map->where($where2)->select();
		foreach($seat2 as $key=>$val){
			$seat=explode('-',$val['seat_number']);
			if(count($seat)==2){
				$seat2[$key]['seat_id']='seat'.($seat[1]+0);
				$seat2[$key]['span']=$seat[1];
			}else{
				$seat2[$key]['seat_id']='seat'.($seat[0]+0);
				$seat2[$key]['span']=$seat[0];
			}
			$where_s['sid']=$val['id'];
			$aaa=$db_seat_map_user->where($where_s)->find();
			$seat2[$key]['u_id']=$aaa['id'];
			if(!empty($aaa['uids'])){$seat2[$key]['users']=$this->get_username($aaa['uids']);}else{$seat2[$key]['users']=$aaa['users'];}
		}
		$this->assign("seat2",$seat2);
		$where2['region_name']=array('neq','工位');
		$seat22=$db_seat_map->where($where2)->select();
		foreach($seat22 as $key=>$val){
			$where_s['sid']=$val['id'];
			$bs=$db_seat_map_user->where($where_s)->select();
			foreach($bs as $kc=>$vc){
				if(!empty($vc['uids'])){$bs[$kc]['users']=$this->get_username($vc['uids']);}
				if(count($bs)>1){
					$bs[$kc]['seat_id']='room_'.$val['seat_number'].($kc+1);
				}else{
					if($val['seat_number']==602){
						$bs[$kc]['seat_id']='room_'.$val['seat_number'].($kc+1);
						$sum2=(count($bs));
						$bs[($sum2+1)]['seat_id']='room_'.$val['seat_number'].'2';
						$bs[($sum2+1)]['sid']=$vc['sid'];
					}else{
						$bs[$kc]['seat_id']='room_'.$val['seat_number'];
					}
				}
				if($val['region_name']=='办公室'){
					$bs[$kc]['rname']=$val['seat_number'];
					$bs[$kc]['em']=1;
				}else{
					$bs[$kc]['rname']=$vc['users'];
				}
				if($val['seat_number']==602 and count($sum2)==1){
					$ss2[]=$bs[$kc];
					$ss2[]=$as[(count($bs))];
				}else{
					$ss2[]=$bs[$kc];
				}
			}
		}
		$this->assign("ss2",$ss2);
		//第三块区
		$where3['region']=3;
		$where3['region_name']='工位';
		$seat3=$db_seat_map->where($where3)->select();
		foreach($seat3 as $key=>$val){
			$seat=explode('-',$val['seat_number']);
			if(count($seat)==2){
				$seat3[$key]['seat_id']='seat'.($seat[1]+0);
				$seat3[$key]['span']=$seat[1];
			}else{
				$seat3[$key]['seat_id']='seat'.($seat[0]+0);
				$seat3[$key]['span']=$seat[0];
			}
			$where_s['sid']=$val['id'];
			$aaa=$db_seat_map_user->where($where_s)->find();
			$seat3[$key]['u_id']=$aaa['id'];
			if(!empty($aaa['uids'])){$seat3[$key]['users']=$this->get_username($aaa['uids']);}else{$seat3[$key]['users']=$aaa['users'];}
		}
		$this->assign("seat3",$seat3);
		$where3['region_name']=array('neq','工位');
		$seat33=$db_seat_map->where($where3)->select();
		foreach($seat33 as $key=>$val){
			$where_s['sid']=$val['id'];
			$cs=$db_seat_map_user->where($where_s)->select();
			foreach($cs as $kc=>$vc){
				if(!empty($vc['uids'])){$cs[$kc]['users']=$this->get_username($vc['uids']);}
				if(count($cs)>1){
					$cs[$kc]['seat_id']='room_'.$val['seat_number'].($kc+1);
				}else{
					if($val['seat_number']==605){
						$cs[$kc]['seat_id']='room_'.$val['seat_number'].($kc+1);
						$sum3=(count($cs));
						$cs[($sum3+1)]['seat_id']='room_'.$val['seat_number'].'2';
						$cs[($sum3+1)]['sid']=$vc['sid'];
					}else{
						$cs[$kc]['seat_id']='room_'.$val['seat_number'];
					}
				}
				if($val['region_name']=='办公室'){
					$cs[$kc]['rname']=$val['seat_number'];
					$cs[$kc]['em']=1;
				}else{
					$cs[$kc]['rname']=$vc['users'];
				}
				if($val['seat_number']==605 and count($sum3)==1){
					$ss3[]=$cs[$kc];
					$ss3[]=$cs[(count($cs))];
				}else{
					$ss3[]=$cs[$kc];
				}
			}
		}
		$this->assign("ss3",$ss3);
		if(get_permission('ZW',NULL,1)){
			$this->assign("zw",1);
		}
		if(get_permission('ZW',1)){
			$this->assign("ck",1);
		}
		$this->display();
	}
	
	//座位表-->ajax designed by:zhaoshihao 2018-02-01
	function seat_map_ajax(){
		$db_seat_map=M('db_seat_map');
		$db_seat_map_user=M('db_seat_map_user');
		$db_seat_map_thing=M('db_seat_map_thing');
		$db_seat_map_record=M('db_seat_map_record');
		$db_user=M('db_user');
		if($_REQUEST['action']=='detail'){
			//区位详情
			$where['id']=$_REQUEST['u_id'];
			$data['detail']=$db_seat_map_user->where($where)->find();
			if(!empty($data['detail'])){
				if(!empty($data['detail']['uids'])){$data['detail']['users']=$this->get_username($data['detail']['uids']);}
				$where_s['id']=$data['detail']['sid'];
				$seat=$db_seat_map->where($where_s)->find();
				$data['region_name']=$seat['region_name'];
				$data['seat_number']=$seat['seat_number'];
				if($data['region_name']=='工位' or $data['region_name']=='办公室'){}else{unset($data['detail']['users']);}
				$where_t['gid']=$_REQUEST['u_id'];
				$where_t['t_type']=1;
				$data['it']=$db_seat_map_thing->where($where_t)->select();
				foreach($data['it'] as $ki=>$vi){
					$data['it'][$ki]['name']=$this->get_db_universal_label(2,$vi['things']);
				}
				$where_t['t_type']=2;
				$data['xz']=$db_seat_map_thing->where($where_t)->select();
				foreach($data['xz'] as $kx=>$vx){
					$data['xz'][$kx]['name']=$this->get_db_universal_label(2,$vx['things']);
				}
				//区位编号
				//$where_d['db_seat_map_user.id']=array('neq',$_REQUEST['u_id']);
				$where_d['id']=array('neq',$_REQUEST['mid']);
				$where_d['region_name']=array('in','工位,办公室');
				
				$zw=$db_seat_map->where($where_d)->select();
				foreach($zw as $ka=>$va){
					$add['mid']=$va['id'];
					$add['region_name']=$va['region_name'];
					$add['seat_number']=$va['seat_number'];
					$where_aa['sid']=$va['id'];
					$count=$db_seat_map_user->where($where_aa)->group('uids')->select();
					if(count($count)>1){
						foreach($count as $km=>$vm){
							$add['id']=$vm['id'];
							if(!empty($vm['uids'])){
								$add['seat_number']=$va['seat_number'].'-'.($km+1).'('.$this->get_username($vm['uids']).')';
							}elseif(!empty($vm['users'])){
								$add['seat_number']=$va['seat_number'].'-'.($km+1).'('.$vm['users'].')';
							}
							$data['zw'][]=$add;
						}
					}else{
						$add['id']=$count[0]['id'];
						if(!empty($count[0]['uids'])){
							$add['seat_number']=$va['seat_number'].'('.$this->get_username($count[0]['uids']).')';
						}elseif(!empty($count[0]['users'])){
							$add['seat_number']=$va['seat_number'].'('.$count[0]['users'].')';
						}
						$data['zw'][]=$add;
					}
				}
			}else{
				$where_u['sid']=array('exp','is NULL');
				$data['user']=$db_seat_map_user->where($where_u)->select();
				foreach($data['user'] as $kk=>$vv){
					if(!empty($vv['uids'])){$data['user'][$kk]['users']=$this->get_username($vv['uids']);}
				}
				$where_s['id']=$_REQUEST['mid'];
				$seat=$db_seat_map->where($where_s)->find();
				$data['region_name']=$seat['region_name'];
				$data['seat_number']=$seat['seat_number'];
			}
			echo json_encode($data);die;
		}elseif($_REQUEST['action']=='find_user'){
			if(empty($_REQUEST['id'])){
				if(empty($_REQUEST['val'])){
					$where_s['username']=array('neq','admin');
				}else{
					$where_s['_string']='(username like "%'.$_REQUEST['val'].'%") and (username!="admin")';
				}
				$where_u['uids']=array('exp','is not NULL');
				$asdasd=$db_seat_map_user->where($where_u)->select();
				foreach($asdasd as $kk=>$vv){
					$uid_str.=$vv['uids'].',';
				}
				$uid=rtrim($uid_str,',');
				$where_s['id']=array('not in',$uid);
				$where_s['status']=1;
				$data=$db_user->field('id,username')->where($where_s)->order('spell')->select();
			}else{
				if(empty($_REQUEST['val'])){
					$where_s['username']=array('neq','admin');
				}else{
					$where_s['_string']='(username like "%'.$_REQUEST['val'].'%") and (username!="admin")';
				}
				$where_u['id']=array('neq',$_REQUEST['id']);
				$where_u['uids']=array('exp','is not NULL');
				$asdasd=$db_seat_map_user->where($where_u)->select();
				foreach($asdasd as $kk=>$vv){
					$uid_str.=$vv['uids'].',';
				}
				$uid=rtrim($uid_str,',');
				$where_s['id']=array('not in',$uid);
				$where_s['status']=1;
				$data=$db_user->field('id,username')->where($where_s)->order('spell')->select();
			}
			echo json_encode($data);die;
		}elseif($_REQUEST['action']=='add'){
			//添加座位人员
			$where['id']=$_REQUEST['u_id'];
			$where['sid']=$_REQUEST['mid'];
			$db_seat_map_user->save($where);
			//记录
			$where_r['types']=1;
			$where_r['sid']=$_REQUEST['mid'];
			$where_r['gid']=$_REQUEST['u_id'];
			$where_r['add_uid']=$this->login_id;
			$where_r['add_date']=date('Y-m-d H:i:s');
			$db_seat_map_record->add($where_r);
			$data['status']=self::YES;
			echo json_encode($data);die;
		}elseif($_REQUEST['action']=='clear'){
			//清除座位人员
			$where['id']=$_REQUEST['u_id'];
			$seat=$db_seat_map_user->where($where)->find();
			$where['sid']=NULL;
			$db_seat_map_user->save($where);
			//记录
			$where_r['types']=3;
			$where_r['sid']=$seat['sid'];
			$where_r['gid']=$_REQUEST['u_id'];
			$where_r['add_uid']=$this->login_id;
			$where_r['add_date']=date('Y-m-d H:i:s');
			$db_seat_map_record->add($where_r);
			$data['status']=self::YES;
			echo json_encode($data);die;
		}elseif($_REQUEST['action']=='change'){
			//座位互换
			$where_o['id']=$_REQUEST['u_id'];
			$where_o['sid']=$_REQUEST['nmid'];
			$db_seat_map_user->save($where_o);
			//记录
			$where_r['types']=2;
			$where_r['sid']=$_REQUEST['nmid'];
			$where_r['gid']=$_REQUEST['u_id'];
			$where_r['add_uid']=$this->login_id;
			$where_r['add_date']=date('Y-m-d H:i:s');
			$db_seat_map_record->add($where_r);
			if($_REQUEST['nu_id']=='null'){
				//新座位没人
			}else{
				//新座位有人
				$where_n['id']=$_REQUEST['nu_id'];
				$where_n['sid']=$_REQUEST['omid'];
				$db_seat_map_user->save($where_n);
				//记录
				$where_r['types']=2;
				$where_r['sid']=$_REQUEST['omid'];
				$where_r['gid']=$_REQUEST['nu_id'];
				$where_r['add_uid']=$this->login_id;
				$where_r['add_date']=date('Y-m-d H:i:s');
				$db_seat_map_record->add($where_r);
			}
			$data['status']=self::YES;
			echo json_encode($data);die;
		}
	}
	
	//座位人员 designed by:zhaoshihao 2018-02-01
	function seat_map_user(){
		$db_seat_map=M('db_seat_map');
		$db_seat_map_user=M('db_seat_map_user');
		$db_seat_map_thing=M('db_seat_map_thing');
		if(!empty($_REQUEST['keyword']))
		{
			$map['users']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['username']=array('like','%'.$_REQUEST['keyword'].'%');
			$map['_logic']='or';
			$where_s['_complex']=$map;
			$this->assign('keyword',$_REQUEST['keyword']);
			$count=$db_seat_map_user->where($where_s)->join('left join db_user on db_seat_map_user.uids=db_user.id')->count();
			$Page = new \Think\Page($count,15);
			$show=$Page->show();
			$result=$db_seat_map_user->field('db_seat_map_user.*')->where($where_s)->join('left join db_user on db_seat_map_user.uids=db_user.id')->limit($Page->firstRow.','.$Page->listRows)->select();
		}else{
			$count=$db_seat_map_user->count();
			$Page = new \Think\Page($count,15);
			$show=$Page->show();
			$result=$db_seat_map_user->limit($Page->firstRow.','.$Page->listRows)->select();
		}
		foreach($result as $key=>$val){
			$seat=$db_seat_map->where('id='.$val['sid'].'')->find();
			$result[$key]['region_name']=$seat['region_name'];
			$result[$key]['seat_number']=$seat['seat_number'];
			if(!empty($val['uids'])){$result[$key]['users']=$this->get_username($val['uids']);}
			$where['gid']=$val['id'];
			$where['t_type']=1;
			$result[$key]['it_thing']=$db_seat_map_thing->where($where)->select();
			foreach($result[$key]['it_thing'] as $k1=>$v1){
				if($k1==0){
					$result[$key]['it_things']=$this->get_db_universal_label(2,$v1['things']).'('.$v1['things_sum'].')';
				}else{
					$result[$key]['it_things'].=','.$this->get_db_universal_label(2,$v1['things']).'('.$v1['things_sum'].')';
				}
				//$result[$key]['it_thing'][$k1]['name']=$this->get_db_universal_label(2,$v1['things']);
			}
			$where['t_type']=2;
			$result[$key]['xz_thing']=$db_seat_map_thing->where($where)->select();
			foreach($result[$key]['xz_thing'] as $k2=>$v2){
				if($k2==0){
					$result[$key]['xz_things']=$this->get_db_universal_label(2,$v2['things']).'('.$v2['things_sum'].')';
				}else{
					$result[$key]['xz_things'].=','.$this->get_db_universal_label(2,$v2['things']).'('.$v2['things_sum'].')';
				}
				//$result[$key]['xz_thing'][$k2]['name']=$this->get_db_universal_label(2,$v2['things']);
			}
			if(empty($val['sid'])){$result[$key]['del']=1;}
		}
		$this->assign('page',$show);
		$this->assign("result",$result);
		$this->display();
	}
	
	//座位人员-->添加 designed by:zhaoshihao 2018-02-01
	function seat_map_user_add(){
		//类别表
		$db_seat_map_user=M('db_seat_map_user');
		$db_seat_map_thing=M('db_seat_map_thing');
		$screen=$this->library_type_list(15,NULL,NULL,1);
		$it=$screen[1]['second'];
		$xz=$screen[2]['second'];
		$this->assign("it",$it);
		$this->assign("xz",$xz);
		if($_REQUEST['action']=='add'){
			$db_user=M('db_user');
			//编辑保存
			$where_s['username']=$_REQUEST['nums1'];
			$has=$db_user->where($where_s)->find();
			if(empty($has)){
				$where['users']=$_REQUEST['nums1'];
			}else{
				$where['uids']=$has['id'];
			}
			if(!empty($_REQUEST['other_goods'])){$where['other_goods']=$_REQUEST['other_goods'];}
			if(!empty($_REQUEST['remark'])){$where['remark']=$_REQUEST['remark'];}
			$id=$db_seat_map_user->add($where);
			
			//
			if(!empty($_REQUEST['it_goods'])){
				$where_i['gid']=$id;
				foreach($_REQUEST['it_goods_sum'] as $ki=>$vi){
					if(!empty($vi)){
						$it_goods_sum[]=$vi;
					}
				}
				foreach($_REQUEST['it_goods'] as $ki=>$vi){
					$where_i['t_type']=1;
					$where_i['things']=$vi;
					$where_i['things_sum']=$it_goods_sum[$ki];
					$db_seat_map_thing->add($where_i);
				}
			}
			if(!empty($_REQUEST['xz_goods'])){
				$where_x['gid']=$id;
				foreach($_REQUEST['xz_goods_sum'] as $ka=>$va){
					if(!empty($va)){
						$xz_goods_sum[]=$va;
					}
				}
				foreach($_REQUEST['xz_goods'] as $kx=>$vx){
					$where_x['t_type']=2;
					$where_x['things']=$vx;
					$where_x['things_sum']=$xz_goods_sum[$kx];
					$db_seat_map_thing->add($where_x);
				}
			}
			/*
			$it_goods=$_REQUEST['it_goods'];
			$it_number=$_REQUEST['it_goods_number'];
			$goods=array_count_values($it_goods);
			foreach($it_goods as $ki=>$vi){
				$good[$ki]['things']=$vi;
				$good[$ki]['things_number']=$it_number[$ki];
			}
			foreach($good as $ka=>$va){
				foreach($good as $kb=>$vb){
					if($vb['things']==$va['things']){
						$good[$ka]['things_number'].=','.$vb['things_number'];
					}
				}
			}
			*/
			$this->success("操作成功",__APP__."/Administration/seat_map_user");die;
		}
		$this->display();
	}
	
	//座位人员-->编辑 designed by:zhaoshihao 2018-02-01
	function seat_map_user_edit(){
		$db_user=M('db_user');
		$db_seat_map=M('db_seat_map');
		$db_seat_map_user=M('db_seat_map_user');
		$db_seat_map_thing=M('db_seat_map_thing');
		$where_d['id']=$_REQUEST['id'];
		$detail=$db_seat_map_user->where($where_d)->find();
		if(!empty($detail['uids'])){$detail['users']=$this->get_username($detail['uids']);}
		//类别表
		$where_it['gid']=$_REQUEST['id'];
		$where_it['t_type']=1;
		$its=$db_seat_map_thing->where($where_it)->select();
		
		$where_it['t_type']=2;
		$xzs=$db_seat_map_thing->where($where_it)->select();
		
		$screen=$this->library_type_list(15,NULL,NULL,1);
		$it=$screen[1]['second'];
		$xz=$screen[2]['second'];
		foreach($it as $k11=>$v11){
			foreach($its as $k111=>$v111){
				if($v111['things']==$v11['id']){
					$it[$k11]['check']=1;
					$it[$k11]['sum']=$v111['things_sum'];
				}
			}
		}
		foreach($xz as $k22=>$v22){
			foreach($xzs as $k222=>$v222){
				if($v222['things']==$v22['id']){
					$xz[$k22]['check']=1;
					$xz[$k22]['sum']=$v222[things_sum];
				}
			}
		}
		$this->assign("it",$it);
		$this->assign("xz",$xz);
		$this->assign("detail",$detail);
		$this->assign("keyword",$keyword);
		if($_REQUEST['action']=='edit'){
			$db_user=M('db_user');
			//编辑保存
			/*
			$where_s['username']=$_REQUEST['nums1'];
			$has=$db_user->where($where_s)->find();
			if(empty($has)){
				$where['users']=$_REQUEST['nums1'];
			}else{
				$where['uids']=$has['id'];
			}
			*/
			if(!empty($_REQUEST['other_goods'])){$where['other_goods']=$_REQUEST['other_goods'];}else{$where['other_goods']=NULL;}
			if(!empty($_REQUEST['remark'])){$where['remark']=$_REQUEST['remark'];}else{$where['remark']=NULL;}
			$where['id']=$_REQUEST['id'];
			$db_seat_map_user->save($where);
			//副表
			$where_de['gid']=$_REQUEST['id'];
			$db_seat_map_thing->where($where_de)->delete();
			if(!empty($_REQUEST['it_goods'])){
				$where_i['gid']=$_REQUEST['id'];
				foreach($_REQUEST['it_goods_sum'] as $ki=>$vi){
					if(!empty($vi)){
						$it_goods_sum[]=$vi;
					}
				}
				foreach($_REQUEST['it_goods'] as $ki=>$vi){
					$where_i['t_type']=1;
					$where_i['things']=$vi;
					$where_i['things_sum']=$it_goods_sum[$ki];
					$db_seat_map_thing->add($where_i);
				}
			}
			if(!empty($_REQUEST['xz_goods'])){
				$where_x['gid']=$_REQUEST['id'];
				foreach($_REQUEST['xz_goods_sum'] as $ka=>$va){
					if(!empty($va)){
						$xz_goods_sum[]=$va;
					}
				}
				foreach($_REQUEST['xz_goods'] as $kx=>$vx){
					$where_x['t_type']=2;
					$where_x['things']=$vx;
					$where_x['things_sum']=$xz_goods_sum[$kx];
					$db_seat_map_thing->add($where_x);
				}
			}
			$this->success("操作成功",__APP__."/Administration/seat_map_user/keyword/".$_REQUEST['keyword']."&p=".$_SESSION['page']);die;
		}
		$this->display();
	}
	
	//座位人员-->ajax designed by:zhaoshihao 2018-02-01
	function seat_map_user_ajax(){
		$db_seat_map=M('db_seat_map');
		$db_seat_map_user=M('db_seat_map_user');
		$db_seat_map_thing=M('db_seat_map_thing');
		$where_d['id']=$_REQUEST['id'];
		$db_seat_map_user->where($where_d)->delete();
		$where_t['gid']=$_REQUEST['id'];
		$db_seat_map_thing->where($where_t)->delete();
		$data['status']=self::YES;
		echo json_encode($data);die;
	}
	
	//座位表类别 designed by:zhaoshihao 2018-02-01
	function seat_type(){
		$db_seat_map_thing=M('db_seat_map_thing');
		$screen=$this->library_type_list(15,NULL,NULL,1);
		$it=$screen[1];
		foreach($it['second'] as $ki=>$vi){
			$where_i['things']=$vi['id'];
			$ha=$db_seat_map_thing->where($where_i)->find();
			if(empty($ha)){$it['second'][$ki]['audit']=1;}
		}
		$result[]=$it;
		$xz=$screen[2];
		foreach($xz['second'] as $kx=>$vx){
			$where_x['things']=$vx['id'];
			$has=$db_seat_map_thing->where($where_x)->find();
			if(empty($has)){$xz['second'][$kx]['audit']=1;}
		}
		$result[]=$xz;
		$this->assign("result",$result);
		$this->display();
	}
	
	//座位表类别-->ajax designed by:zhaoshihao 2018-02-01
	function seat_type_ajax(){
		$db_seat_map=M('db_seat_map');
		$db_seat_map_user=M('db_seat_map_user');
		$db_seat_map_thing=M('db_seat_map_thing');
		$db_universal_label1=M('db_universal_label1');
		$db_universal_label2=M('db_universal_label2');
		if($_REQUEST['action']=='doadd'){
			$where['first_id']=$_REQUEST['first'];
			$where['second_name']=$_REQUEST['second'];
			$has=$db_universal_label2->where($where)->find();
			if(!empty($has)){
				$data['status']=self::NO;
				$data['msg']='已有相同物品，请勿重复添加';
				echo json_encode($data);die;
			}else{
				$second_sort=$db_universal_label2->where('first_id='.$_REQUEST['first'].'')->max('second_sort');
				$where['second_sort']=$second_sort+1;
				$where['add_uid']=$this->login_id;
				$where['add_date']=date('Y-m-d H:i:s');
				$db_universal_label2->add($where);
				$data['status']=self::YES;
				echo json_encode($data);die;
			}
		}elseif($_REQUEST['action']=='doedit'){
			$where['id']=$_REQUEST['id'];
			$name=$db_universal_label2->where($where)->find();
			$where_s['first_id']=$name['first_id'];
			$where_s['second_name']=$_REQUEST['name'];
			$second=$db_universal_label2->where($where_s)->find();
			if(empty($second)){
				$where['second_name']=$_REQUEST['name'];
				$db_universal_label2->save($where);
				$data['status']=self::YES;
				echo json_encode($data);die;
			}else{
				$data['status']=self::NO;
				if($_REQUEST['name']==$name['second_name']){
					$data['msg']='分类名未改动';
				}else{
					$data['msg']='该分类已存在';
				}
				echo json_encode($data);die;
			}
		}elseif($_REQUEST['action']=='delete'){
			$where_s['id']=$_REQUEST['id'];
			$desecond=$db_universal_label2->where($where_s)->find();
			if($_REQUEST['bool']!=1){
				$where['things']=$_REQUEST['id'];
				$thing=$db_seat_map_thing->where($where)->group('gid')->select();
				foreach($thing as $ke=>$ve){
					$where_c['things']=$_REQUEST['cid'];
					$where_c['gid']=$ve['gid'];
					$cthing=$db_seat_map_thing->where($where_c)->find();
					if(empty($cthing)){
						$where_cc['id']=$ve['id'];
						$where_cc['things']=$_REQUEST['cid'];
						$db_seat_map_thing->save($where_cc);
					}else{
						$where['gid']=$ve['gid'];
						$db_seat_map_thing->where($where)->delete();
					}
				}
			}
			//删二级分类
			$db_universal_label2->where($where_s)->delete();
			//二级分类重新排序
			$where_sort['first_id']=$desecond['first_id'];
			$arr=$db_universal_label2->where($where_sort)->order('second_sort')->select();
			foreach($arr as $key=>$value){
				$where_nt['id']=$value['id'];
				$where_nt['second_sort']=$key+1;
				$db_universal_label2->save($where_nt);
			}
			$data['status']=self::YES;
			echo json_encode($data);die;
		}elseif($_REQUEST['action']=='detail'){
			$data=$db_seat_map_user->select();
			foreach($data as $key=>$val){
				if(!empty($val['uids'])){$data[$key]['users']=$this->get_username($val['uids']);}
				$where['things']=$_REQUEST['id'];
				$where['gid']=$val['id'];
				$thing=$db_seat_map_thing->where($where)->find();
				if(!empty($thing)){
					$data[$key]['check']=1;
				}
			}
			echo json_encode($data);die;
		}elseif($_REQUEST['action']=='dodetail'){
			foreach($_REQUEST['main_id'] as $key=>$val){
				$where_s['gid']=$val;
				$where_s['things']=$_REQUEST['second_id'];
				$things=$db_seat_map_thing->where($where_s)->find();
				if(empty($things)){
					$where_a['gid']=$val;
					if($_REQUEST['first_id']=='58'){
						$where_a['t_type']=1;
					}else{
						$where_a['t_type']=2;
					}
					$where_a['things']=$_REQUEST['second_id'];
					$where_a['things_sum']=1;
					//$where_a['things_sum']=$_REQUEST['sum'];
					$db_seat_map_thing->add($where_a);
				}
			}
			//取消选择
			$id=join(',',$_REQUEST['main_id']);
			$where_i['id']=array('not in',$id);
			$qx=$db_seat_map_user->where($where_i)->select();
			foreach($qx as $kk=>$vv){
				$where_q['gid']=$vv['id'];
				$where_q['things']=$_REQUEST['second_id'];
				$things=$db_seat_map_thing->where($where_q)->find();
				if(!empty($things)){
					$where_de['id']=$things['id'];
					$db_seat_map_thing->where($where_de)->delete();
				}
			}
			$data['status']=self::YES;
			echo json_encode($data);die;
		}
	}
	
	//登录页展示 designed by:zhaoshihao 2018-02-01
	function login_show(){
		$db_user=M('db_user');
		$db_login=M('db_login');
		
		//同步人员
		$where_u['status']=1;
		$where_u['id']=array('neq',1);
		$user=$db_user->where($where_u)->select();
		foreach($user as $k1=>$v1){
			$users[]=$v1['id'];
		}
		$login=$db_login->select();
		foreach($login as $k2=>$v2){
			unset($where_de);
			$where_d['id']=$v2['uid'];
			$del=$db_user->where($where_d)->find();
			if($del['status']==-1){
				$where_de['id']=$v2['id'];
				$where_de['sta']=-1;
				$where_de['remark']=NULL;
				$where_de['date']=NULL;
				$db_login->save($where_de);
			}elseif($del['status']==1){
				$where_de['id']=$v2['id'];
				$where_de['sta']=1;
				$db_login->save($where_de);
			}
		}
		$where_t['sta']=1;
		$login=$db_login->where($where_t)->select();
		foreach($login as $k2=>$v2){
			$logins[]=$v2['uid'];
		}
		$name=array_merge(array_diff($users,$logins),array_diff($logins,$users));
		foreach($name as $kk=>$vv){
			$where_ff['id']=$vv;
			$name=$db_user->where($where_ff)->find();
			if(!empty($name['username1'])){
				$where_a['name']=$name['username1'];
			}else{
				$where_a['name']=$name['username'];
			}
			$where_a['uid']=$vv;
			$where_a['sta']=1;
			$max=$db_login->max('date');
			$da=$max+3600*24;
			$re=date('Y-m-d',($max+3600*24));
			if(date('w',$da)==0 or date('w',$da)==6){
				for($i=2;$i<100;$i++){
					$das=$da+$i*3600*24;
					if(date('w',$das)==0 or date('w',$das)==6){
						continue;
					}else{
						$where_a['date']=$das;
						$where_a['remark']=date('Y-m-d',$das);
						break;
					}
				}
			}else{
				$where_a['date']=$da;
				$where_a['remark']=$re;
			}
			
		
		
			$db_login->add($where_a);
		}
		
		//列表
		if($_REQUEST['username']!=-10 and !empty($_REQUEST['username']))
		{
			$where_s['sta']=1;
			$where_s['db_login.uid']=$_REQUEST['username'];
			$this->assign('username',$_REQUEST['username']);
			$count=$db_login->where($where_s)->join('left join db_user on db_login.uid=db_user.id')->count();
			$Page = new \Think\Page($count,15);
			$show=$Page->show();
			$result=$db_login->field('db_login.*')->where($where_s)->join('left join db_user on db_login.uid=db_user.id')->limit($Page->firstRow.','.$Page->listRows)->order('date')->select();
		}else{
			$where_s['sta']=1;
			$this->assign('username',-10);
			$count=$db_login->join('left join db_user on db_login.uid=db_user.id')->where($where_s)->count();
			$Page = new \Think\Page($count,15);
			$show=$Page->show();
			$result=$db_login->field('db_login.*')->where($where_s)->join('left join db_user on db_login.uid=db_user.id')->limit($Page->firstRow.','.$Page->listRows)->order('date')->select();
		}
		foreach($result as $key=>$val){
			$where_f['id']=$val['uid'];
			$name=$db_user->where($where_f)->find();
			if(!empty($name['username1'])){
				$result[$key]['name']=$name['username1'];
			}else{
				$result[$key]['name']=$name['username'];
			}
		}
		$this->assign('page',$show);
		$this->assign("result",$result);
		//姓名下拉菜单
		$this->assign('result1',$this->get_all_username(1));
		$this->display();
	}
	
	//登录页展示-->ajax designed by:zhaoshihao 2018-02-01
	function login_show_ajax(){
		$db_login=M('db_login');
		$db_user=M('db_user');
		if($_REQUEST['action']=='query'){
			if(!empty($_REQUEST['remark'])){
				$where['date']=strtotime($_REQUEST['remark']);
				$where['id']=array('neq',$_REQUEST['id']);
				$data=$db_login->where($where)->find();
			}
			if(empty($data)){
				$data['type']=1;
			}else{
				$where_f['id']=$data['uid'];
				$name=$db_user->where($where_f)->find();
				if(!empty($name['username1'])){
					$data['name']=$name['username1'];
				}else{
					$data['name']=$name['username'];
				}
				$data['type']=-1;
			}
			echo json_encode($data);die;
		}elseif($_REQUEST['action']=='edit'){
			if(!empty($_REQUEST['remark'])){
				$where['date']=strtotime($_REQUEST['remark']);
				$where['id']=array('neq',$_REQUEST['id']);
				$has=$db_login->where($where)->find();
			}
			
	
	
			//日期是否有人
			if(empty($has)){
				$where['id']=$_REQUEST['id'];
				if(!empty($_REQUEST['remark'])){$where['remark']=$_REQUEST['remark'];}else{$where['remark']=NULL;}
				$where['hello_img']=$_REQUEST['hello_img'];
				$where['content']=$_REQUEST['content'];
				if(!empty($_REQUEST['remark'])){$where['date']=strtotime($_REQUEST['remark']);}else{$where['date']=NULL;}
				/* 员工展示修改时开放修改照片 add by chenyan 2018-08-24 */
				if($_REQUEST['img']){
					$where['img']=$_REQUEST['img'];
					//删除原来的图片
					$where_old['id']=$_REQUEST['id'];
					$old=$db_login->where($where_old)->find();
					$file1=str_replace("/dms_erp/uploads", "./uploads",$old['img']);
					if (file_exists($file1)){
						@unlink($file1);
					}
				}

				$db_login->save($where);
				//日志
				$db_login_log=M('db_login_log');
				$where_l['mid']=$_REQUEST['id'];
				$where_l['remark']=$_REQUEST['remark'];
				$where_l['add_uid']=$this->login_id;
				$where_l['add_date']=date('Y-m-d H:i:s');
				$db_login_log->add($where_l);
				
			}else{
				//调换操作
				$where_n['id']=$_REQUEST['id'];
				$now=$db_login->where($where_n)->find();
				$where_s['id']=$has['id'];
				$where_s['remark']=$now['remark'];
				$where_s['date']=$now['date'];
				$db_login->save($where_s);
				//日志
				$db_login_log=M('db_login_log');
				$where_l['mid']=$has['id'];
				$where_l['remark']=$now['remark'];
				$where_l['add_uid']=$this->login_id;
				$where_l['add_date']=date('Y-m-d H:i:s');
				$db_login_log->add($where_l);
				//调换操作
				$where_n['hello_img']=$_REQUEST['hello_img'];
				$where_n['content']=$_REQUEST['content'];
				$where_n['remark']=$_REQUEST['remark'];
				$where_n['date']=strtotime($_REQUEST['remark']);
				$db_login->save($where_n);
				//日志
				$db_login_log=M('db_login_log');
				$where_ll['mid']=$_REQUEST['id'];
				$where_ll['remark']=$_REQUEST['remark'];
				$where_ll['add_uid']=$this->login_id;
				$where_ll['add_date']=date('Y-m-d H:i:s');
				$db_login_log->add($where_ll);
			}
			$data['status']=self::YES;
			echo json_encode($data);die;
		}
	}
}
?>